// pages/bindphone/bindphone.js
var app = getApp()
var config = require('../../utils/config.js');
var netutil = require('../../utils/netutil.js');
var baseutil = require('../../utils/baseutil.js');
Page({

  /**
    * 页面的初始数据
    */
  data: {
    verifyCodeTime: '获取验证码',
    openId: null,

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOpenID();
    var THIS = this

    //创建可重复使用的toast实例，并附加到this上，通过this.wetoast访问
    new app.WeToast()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getOpenID: function () {
    this.setData({
      openId: app.globalData.openid
    })
  },
  formSubmit: function (e) {//提交参数  进行绑定
    var userName = e.detail.value.userName;
    var code = e.detail.value.code;
    console.log('form发生了submit事件，携带数据为：', e.detail.value.userName + "---" + e.detail.value.code)
    if (userName == null || userName.length == 0) {
      this.wetoast.toast({
        title: '手机号码不能为空！',
        duration: 1000
      })
      return;
    }
    if (code == null || code.length == 0) {
      this.wetoast.toast({
        title: '验证码不能为空！',
        duration: 1000
      })
      return;
    }

    var THIS = this
    var url = config.getBaseUrl("GloUser", "BindXCXPhone");
    var Data = new Object();
    Data.Phone = userName;
    Data.ValidateCode = code;
    Data.OpenID = this.data.openId;

    baseutil.log(url);
    netutil.post(
      url, Data,
      function (res) {
        console.log(res.data)

        if (res.data.ResponseMetaInfo.StatusCode == '0') {
          var Data = res.data.Data;
          app.globalData.TUserInfo = Data
          app.globalData.isLogin = true
          // baseutil.saveUserInfo(Data)


          THIS.wetoast.toast({
            title: "绑定成功!",
            title: res.data.ResponseMetaInfo.Reason,
            duration: 1000
          })
          var pages = getCurrentPages();
          var prePage = pages[pages.length - 2];  //上一个页面
          try { prePage.updateData(Data); } catch (e) { }
          finally {
            setTimeout(function () {
              baseutil.returnLastPage()
            }, 500);
          }



        } else {
          THIS.wetoast.toast({
            title: res.data.ResponseMetaInfo.Reason,
            duration: 1000
          })
        }
      },
      function (res) {

      }
    )
  },
  //page中添加属性（事件）
  mobileInputEvent: function (e) {//判断获取验证码的手机账号
    this.setData({
      mobile: e.detail.value
    })
  },
  verifyCodeEvent: function (e) {
    if (this.data.buttonDisable) return false;
    var mobile = this.data.mobile;
    var regMobile = /^1\d{10}$/;
    if (!regMobile.test(mobile)) {
      this.wetoast.toast({
        title: '手机号码有误！',
        duration: 1000
      })
      return false;
    }
    var that = this;
    var c = 60;
    that.setData({
      verifyCodeTime: c + 's后重发',
      buttonDisable: true
    })
    var intervalId = setInterval(function () {
      c = c - 1;
      that.setData({
        verifyCodeTime: c + 's后重发',
        buttonDisable: true
      })
      if (c == 0) {
        clearInterval(intervalId);
        that.setData({
          verifyCodeTime: '获取验证码',
          buttonDisable: false
        })
      }
    }, 1000)

    that.getCode(mobile);
  },
  getCode: function (mobile) {//获取验证码
    var THIS = this
    var url = config.getBaseUrl("GloUser", "SendXCXPhoneBindValidateCode");
    var Data = new Object();
    Data.Phone = mobile;

    baseutil.log(url);
    netutil.post(
      url, Data,
      function (res) {
        baseutil.log(res.data)
        if (res.data.ResponseMetaInfo.StatusCode == '0') {

          THIS.wetoast.toast({
            title: '验证码发送成功!',
            title: res.data.ResponseMetaInfo.Reason,
            duration: 1000
          })
        } else {
          THIS.wetoast.toast({
            title: res.data.ResponseMetaInfo.Reason,
            duration: 1000
          })
        }
      },
      function (res) {

      }
    )
  },

})